/**
 *
 */
package com.elcom.emocbc.cbm;

import java.util.List;
import java.util.concurrent.SynchronousQueue;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.elcom.emocbc.db.CellBroadcastDAO;
import com.elcom.emocbc.util.ActionThread;

/**
 * @author tiamo114
 *
 */
public class BroadcastThread extends ActionThread {

    private final Log LOG = LogFactory.getLog(BroadcastThread.class);
    private CellBroadcastDAO dao = null;
    private SynchronousQueue<BroadcastDTO> q = null;

    /**
     * @param name
     */
    public BroadcastThread(String name, SynchronousQueue<BroadcastDTO> _q) {
        super(name);
        this.dao = new CellBroadcastDAO();
        this.q = _q;
    }

    /*
     * (non-Javadoc)
     *
     * @see com.elcom.livescreen.util.ActionThread#action()
     */
    @Override
    protected void action() throws Exception {
        LOG.info("CellbroadCast is running!!!");

        List<BroadcastDTO> listBroadcast = dao.getListBroadcast();
        for (BroadcastDTO broadcastDTO : listBroadcast) {
            this.q.offer(broadcastDTO);
            LOG.info("Add Broadcast request into queue: " + broadcastDTO.toString());
        }
    }

    /*
     * (non-Javadoc)
     *
     * @see
     * com.elcom.livescreen.util.ActionThread#onException(java.lang.Exception)
     */
    @Override
    protected void onException(Exception e) {
        LOG.error(this.getName() + " is onException: ", e);
    }

    /*
     * (non-Javadoc)
     *
     * @see com.elcom.livescreen.util.ActionThread#onExecuting()
     */
    @Override
    protected void onExecuting() throws Exception {
        LOG.info(this.getName() + " is onExecuting");
    }

    /*
     * (non-Javadoc)
     *
     * @see com.elcom.livescreen.util.ActionThread#onKilling()
     */
    @Override
    protected void onKilling() {
        LOG.info(this.getName() + " is onKilling");
    }

    /*
     * (non-Javadoc)
     *
     * @see com.elcom.livescreen.util.ActionThread#sleeptime()
     */
    @Override
    protected long sleeptime() throws Exception {
        return CellBroadcastService.cycleGetBroadcast * 60 * 1000;
    }
}
